package com.snnsh.kjb.lottery.service;

import com.snnsh.kjb.lottery.dao.IProductDao;
import com.snnsh.kjb.lottery.po.ProductPO;
import com.snnsh.kjb.lottery.vo.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * packageName com.snnsh.kjb.lottery.service
 *
 * @author wick
 * @version V1.0.0
 * @date 2025/5/27
 */
@Service
public class ProductService {

    @Autowired
    private IProductDao productDao;

    public List<ProductVO> queryList(Map<String,Object> params) {
        List<ProductPO> list = productDao.list(params);
        List<ProductVO> collect = list.stream().map(v -> {
            return v.toVo();
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * @description 删除产品
     * @param id:
     * @return void
     * @author wick
     * @date 2025/5/27
     */
    public void deleteById(Integer id){
        productDao.deleteProduct(id);
    }
    /**
     * @description
     * @param productVO:
     * @return void
     * @author wick
     * @date 2025/5/27
     */
    public void addProduct(ProductVO productVO){
        ProductPO productPO = new ProductPO();
        BeanUtils.copyProperties(productVO,productPO);
        //初始化剩余数量
        productPO.setLeftAmount(productPO.getAmount());
        productDao.addProduct(productPO);
    }

    public void updateProduct(ProductVO productVO) {
        ProductPO lastProduct = productDao.findById(productVO.getId());
        BeanUtils.copyProperties(productVO,lastProduct,new String[]{"amount","leftAmount"});
        productDao.updateProduct(lastProduct);
    }
}
